import { login } from '../../api/client'

const client = {
    state:
    sessionStorage.getItem('client') ? 
    JSON.parse(sessionStorage.getItem('client')):
    {
        clientToken:''
    },
    mutations:{
        SET_TOKEN:(state,token)=>{
            state.clientToken = token
        },
        SAVE_CLIENT:(state)=>{
            sessionStorage.setItem('client',state)
        }
    },
    actions:{
        Login({commit},userinfo){
            return new Promise((resolve,reject)=>{
                login(userinfo).then(res=>{
                    console.log(res)
                    commit('SET_TOKEN',res.token)
                    commit('SAVE_CLIENT')
                },err=>{
                    console.log(err)
                    reject(err)
                }).catch(err=>{
                    console.log(err)
                    reject(err)
                })
            })
        },



    }
}

export default client


//  登录api与vuex搭配示例
//    this.$store.dispatch('Login', parmas).then((res) => {
//           this.loading = false;
//           this.$store.dispatch('delAllViews'); // 清除打开过的tabs
//           if (res.isFirstLogin === 1) { // 首次登录，设置密码
//             this.$router.push({ path: '/setlogin', query: { userType: res.type, phone: res.phone } });
//           } else {
//             this.$router.push({ path: '/' });
//           }
//         }).catch((err) => {
//           if (err.code === 5018) {
//             this.countError += 1;
//           }
//           this.loading = false;
//           this.errMsg = err.msg;
//           this.errToast(err.msg);
//         });
